//迭代器,生成指定数量的有序数字
let genNum = {
  size: 1,
  [Symbol.iterator]() {
    let size = this.size;
    let i = 0;
    return {
      next() {
        i++;
        let obj: any = {
          value: i
        };
        if (i > size) {
          obj.done = true;
        } else {
          obj.done = false;
        }
        return obj;
      }
    };
  }
};

//获取指定数字数量的数组
const getList = (size: number) => {
  genNum.size = size;
  return [...genNum] as number[];
};

//月份数组规定12个月
const mss = getList(12);
//时数组从0开始到23
const hss = getList(23);
hss.unshift(0);

//分/秒数组固定60
const minss = getList(60);

type Result = [
  { ys: number[]; currentY: number },
  { ms: number[]; currentM: number },
  { ds: number[]; currentD: number },
  { hs: number[]; currentH: number },
  { mins: number[]; currentMin: number },
  { ss: number[]; currentS: number }
];
type GetData = (data: Date) => Result;

export const getDate: GetData = data => {
  const ys = [data.getFullYear()];
  const currentY = ys[0];
  for (let i = 1; i <= 20; i++) {
    ys.push(currentY + i);
  }
  for (let i = 1; i <= 20; i++) {
    ys.unshift(currentY - i);
  }

  //月份显示数组
  const ms = mss;
  //当前月份
  const currentM = data.getMonth() + 1;

  //根据年月来生成日数组
  const ds = _getCurrentMonthDays(currentY, currentM);
  const currentD = data.getDate();

  const hs = hss;
  const currentH = data.getHours();

  const mins = minss;
  const currentMin = data.getMinutes();

  const ss = minss;
  const currentS = data.getSeconds();

  return [
    { ys, currentY },
    { ms, currentM },
    { ds, currentD },
    { hs, currentH },
    { mins, currentMin },
    { ss, currentS }
  ];
};

//得到一年的某个月有多少天
function _getCurrentMonthDays(inputYear: number, inputMonth: number) {
  let year = inputYear;
  let month = inputMonth - 1 || 1; //如果设置为12月就会增加一年导致变成0月,如果为0月就变成一月
  let isRn = false;
  let days = 0;
  if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
    // 判断是否为闰年：能被400整除或者能被4整除，但不能被100整除
    isRn = true;
  } else {
    isRn = false;
  }
  if (
    month === 1 ||
    month === 3 ||
    month === 5 ||
    month === 7 ||
    month === 8 ||
    month === 10 ||
    month === 12
  ) {
    // 1、3、5、7、8、10、12 每月31天
    days = 31;
  } else if (month == 2) {
    if (isRn) {
      // 为闰年，则多一天为29天
      days = 29;
    } else {
      days = 28;
    }
  } else if (month == 4 || month == 6 || month == 9 || month == 11) {
    // 4、6、9、10、11
    days = 30;
  }

  return getList(days);
}

//在不足10的时间前面加一个0
const to0 = (n: number) => (n < 10 ? "0" + n : "" + n);
//吧format字符串的时间部分替换为真正的时间
export const toDate = (data: Date, str: string) => {
  const YYYY = data.getFullYear() + "";
  const MM = to0(data.getMonth());
  const DD = to0(data.getDate());

  const hh = to0(data.getHours());
  const mm = to0(data.getMinutes());
  const ss = to0(data.getSeconds());
  str = str.replace("YYYY", YYYY);
  str = str.replace("MM", MM);
  str = str.replace("DD", DD);
  str = str.replace("hh", hh);
  str = str.replace("mm", mm);
  str = str.replace("ss", ss);
  return str;
};
